756711
@@ -443,8 +443,8 @@
private static boolean areMergeable(ParseContext pctx, SharedWorkOptimizerCache
         Set<Operator<?>> ascendants =
             findAscendantWorkOperators(pctx, optimizerCache, op);
         if (ascendants.contains(tsOp2)) {
-          dppsOp1.remove(i);
-          i--;
+          // This should not happen, we cannot merge
+          return false;
         }
       }
     }
@@ -454,8 +454,8 @@
private static boolean areMergeable(ParseContext pctx, SharedWorkOptimizerCache
         Set<Operator<?>> ascendants =
             findAscendantWorkOperators(pctx, optimizerCache, op);
         if (ascendants.contains(tsOp1)) {
-          dppsOp2.remove(i);
-          i--;
+          // This should not happen, we cannot merge
+          return false;
         }
       }
     }
@@ -464,9 +464,9 @@
private static boolean areMergeable(ParseContext pctx, SharedWorkOptimizerCache
       return false;
     }
     // Check if DPP branches are equal
+    BitSet bs = new BitSet();
     for (int i = 0; i < dppsOp1.size(); i++) {
       Operator<?> dppOp1 = dppsOp1.get(i);
-      BitSet bs = new BitSet();
       for (int j = 0; j < dppsOp2.size(); j++) {
         if (!bs.get(j)) {
           // If not visited yet
@@ -478,7 +478,7 @@
private static boolean areMergeable(ParseContext pctx, SharedWorkOptimizerCache
           }
         }
       }
-      if (bs.cardinality() == i) {
+      if (bs.cardinality() < i + 1) {
         return false;
       }
     }
@@ -530,7 +530,6 @@
private static SharedResult extractSharedOptimizationInfo(ParseContext pctx,
         if (currentOp1.getChildOperators().size() > 1 ||
                 currentOp2.getChildOperators().size() > 1) {
           // TODO: Support checking multiple child operators to merge further.
-          discardableInputOps.addAll(gatherDPPBranchOps(pctx, optimizerCache, discardableInputOps));
           discardableInputOps.addAll(gatherDPPBranchOps(pctx, optimizerCache, discardableOps));
           discardableInputOps.addAll(gatherDPPBranchOps(pctx, optimizerCache, retainableOps, discardableInputOps));
           return new SharedResult(retainableOps, discardableOps, discardableInputOps, dataSize, maxDataSize);
@@ -539,7 +538,6 @@
private static SharedResult extractSharedOptimizationInfo(ParseContext pctx,
         currentOp2 = currentOp2.getChildOperators().get(0);
       } else {
         // Bail out
-        discardableInputOps.addAll(gatherDPPBranchOps(pctx, optimizerCache, discardableInputOps));
         discardableInputOps.addAll(gatherDPPBranchOps(pctx, optimizerCache, discardableOps));
         discardableInputOps.addAll(gatherDPPBranchOps(pctx, optimizerCache, retainableOps, discardableInputOps));
         return new SharedResult(retainableOps, discardableOps, discardableInputOps, dataSize, maxDataSize);
